package com.example.big_event.controller;


import com.example.big_event.pojo.Article;
import com.example.big_event.pojo.PageBean;
import com.example.big_event.pojo.Result;
import com.example.big_event.service.impl.ArticleServiceImpl;
import com.example.big_event.utils.JwtUtil;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleServiceImpl articleService;
//新增文章
    @PostMapping
    public Result add(@RequestBody @Validated Article article){
        articleService.add(article);
        return Result.success();
    }
    @GetMapping("/detail")
//根据id查找具体文章
    public Result detail(Integer id){
        Article article = articleService.detail(id);
        return Result.success(article);
    }
//更新具体文章信息
    @PutMapping
    public Result update(@RequestBody @Validated Article article){
        articleService.update(article);
        return Result.success();
    }
//删除文章信息
    @DeleteMapping
    public Result delete(Integer id){
        articleService.delete(id);
        return Result.success();
    }
//获取分页信息（包含按照id和状态查找文章信息）
    @GetMapping
    public Result<PageBean<Article>> list(Integer pageNum,
                                          Integer pageSize,
                                          @RequestParam(required = false) String categoryId,
                                          @RequestParam(required = false) String state){
        if(categoryId.isEmpty()){
            categoryId=null;
        }
        if(state.isEmpty()){
            state=null;
        }
        PageBean<Article> pageBean = articleService.list(pageNum, pageSize, categoryId, state);
        System.out.println(pageBean);
        return Result.success(pageBean);
    }

    /*@GetMapping("/list")
    public Result<String> list(@RequestHeader(name = "Authorization") String token, HttpServletResponse response){
//        验证token
//        try {
//            Map<String, Object> claims = JwtUtil.parseToken(token);
//            return Result.success("所有文章的数据");
//        } catch (Exception e){
//            //http响应状态码为401
//            response.setStatus(401);
//            return Result.error("未登录");
//        }

        return Result.success("获取全部文章数据");
    }*/
}
